package com.xiaotu.spider.project.pipeline;

import com.xiaotu.common.model.NetTVModel;
import com.xiaotu.common.util.*;
import com.xiaotu.spider.APipeline;
import com.xiaotu.storage.project.NetTVStorageProcessor;
import net.sf.json.JSONObject;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 网剧数据存储实体类，各个网剧爬取任务，都通过此类进行存储
 *
 * @auther GaoHJ
 * @create 2017/10/11
 */
@Component("NetTVFilePipeline")
@Scope("prototype")
public class NetTVFilePipeline extends APipeline
{
    private final String fileName = NetTVFilePipeline.class.getResource("").getPath() + File
            .separator + System.currentTimeMillis() + ".csv";

    /**
     * 将NetTVModel转储成csv文件，格式：名称|网站|集数|导演|演员|地区|类型|语言|年份|简介|播放量|得分
     *
     * @param resultItems
     * @param task
     */
    @Override
    protected void doProcess(ResultItems resultItems, Task task)
    {
        NetTVModel tv = resultItems.get(NetTVModel.class.getName());
        if (tv != null)
            this.writeFile(tv);
        List<NetTVModel> tvList = resultItems.get(NetTVModel.class.getName() + "List");
        if (tvList != null)
            tvList.forEach(tvModel -> this.writeFile(tvModel));
    }

    private void writeFile(NetTVModel tv)
    {
        JSONObject tvObj = JSONObject.fromObject(tv);
        List<Object> list = new ArrayList<>();
        Arrays.stream(NetTVStorageProcessor.FIELD_ARR).limit(NetTVStorageProcessor.FIELD_ARR
                .length - 1).forEach(field -> list.add(tvObj.get(field)));
        FileUtil.writeFile(fileName, StringUtil.list2CSVLine(list, SepratorUtil.SEP_PIPE) +
                SepratorUtil.LINE_SEPRATOR);
    }
}
